$(function() {
	//下拉菜单
	Common.drop_down_menu_select();
	Common.init_drop_down_menu();
	Common.click_down_menu_list();
	Common.cookie_login();
	Common.topClick();

	supportPlaceholder = 'placeholder' in document.createElement('input');
	//当浏览器不支持placeholder属性时，调用placeholder函数
	if(!supportPlaceholder) {

		$('input').each(function() {

			if($(this).attr("type") == "text" || $(this).attr("type") == "password") {

				placeholder($(this));
			}
		});
	}

	function placeholder(input) {

		var text = input.attr('placeholder'),
			defaultValue = input.defaultValue;

		if(!defaultValue) {
			input.val(text).addClass("phcolor");
		}

		input.focus(function() {

			if(input.val() == text) {

				$(this).val("");
			}
		});

		input.blur(function() {

			if(input.val() == "") {

				$(this).val(text).addClass("phcolor");
			}
		});

		//输入的字符不为灰色
		input.keydown(function() {

			$(this).removeClass("phcolor");
		});
	};

	// 产品加载
	function loadProduct() {
		$('.product_list>div').each(function() {
			$(this).append()
		})
	}

})
var Common = {
	//接口地址
	url_api: 'http://www.ibabypet.com.cn/public/index.php/api/index/pchome', //品牌，类别
	init_drop_down_menu: function() {
		var that = this;
		$.ajax({
			type: "get",
			url: that.url_api,
			async: true,
			success: function(res) {
				res = JSON.parse(res);
				if(res.code == 200) {
					var data = res.data;
					//所有商品下拉菜单渲染
					var brands_data = data.brands;
					var fuc_data = data.attributes;
					//按品牌
					$.each(brands_data, function(i, v) {
						var li_el;
						li_el = '<dd data-id="' + v.id + '">' + v.name + '</dd>';
						$('.down_menu_right[belongto="dog"] dl').eq(0).append(li_el);
						$('.down_menu_right[belongto="cat"] dl').eq(0).append(li_el);
						$('.down_menu_right[belongto="pet"] dl').eq(0).append(li_el);

					});
					//按功能
					$.each(fuc_data, function(i, v) {
						var li_el;
						li_el = '<dd data-id="' + v.id + '">' + v.name + '</dd>';
						$('.down_menu_right[belongto="dog"] dl').eq(1).append(li_el);
						$('.down_menu_right[belongto="cat"] dl').eq(1).append(li_el);
						$('.down_menu_right[belongto="pet"] dl').eq(1).append(li_el);

					});

				} else {
				}
			},
			error: function(res) {
			}
		});
	},
	//全部商品下拉菜单
	drop_down_menu_select: function() {
		//	hover事件绑定
		$(document).on('mouseover mouseout', '.drop_down_menu li', function(event) {
			if(event.type == 'mouseover') {
				$('.drop_down_menu').stop().animate({
					height: '440px'
				}, 400);
				var belongto = $(this).attr('belongto');
				$(this).addClass('active').siblings('li').removeClass('active');
				$('.down_menu_right[belongto="' + belongto + '"]').stop().animate({
					width: '700px'
				}, 50);
			} else {
				$('.drop_down_menu').stop().animate({
					height: '0px'
				}, 1000);
				var belongto = $(this).attr('belongto');
				$(this).removeClass('active');
				$('.down_menu_right[belongto="' + belongto + '"]').stop().animate({
					width: '0px'
				}, 50);
			}
		})

		$(document).on('mouseover mouseout', '.whole_goods_menu', function(event) {
			if(event.type == 'mouseover') {
				$('.drop_down_menu').stop().animate({
					height: '440px'
				}, 400);

			} else {
				$('.drop_down_menu').stop().animate({
					height: '0px'
				}, 400);

			}
		})

		// 下拉菜单的右边菜单
		$(document).on('mouseover mouseout', '.down_menu_right', function(event) {
			if(event.type == 'mouseover') {
				$('.drop_down_menu').stop().animate({
					height: '440px'
				}, 400);

				var belongto = $(this).attr('belongto');
				$('.down_menu_right[belongto="' + belongto + '"]').stop().animate({
					width: '700px'
				}, 50);

				$('.drop_down_menu li[belongto="' + belongto + '"]').addClass('active').siblings().removeClass('active');
			} else {
				$('.drop_down_menu').stop().animate({
					height: '0px'
				}, 400);

				var belongto = $(this).attr('belongto');
				$('.down_menu_right[belongto="' + belongto + '"]').stop().animate({
					width: '0px'
				}, 50);

				$('.drop_down_menu li[belongto="' + belongto + '"]').removeClass('active')
			}
		})
	},
	//点击下拉菜单的品牌 功能跳到商品也页面
	click_down_menu_list: function() {
		$(document).on('click', '.down_menu_right dl dd', function() {
			//获取 belong: 分类、category： 商标||功能  index()方法获取点击的相对同级的第几个元素
			var belong, category, data_id, data_text;
			var index = $(this).parent('dl').index();
			if(index == 0) {
				category = 1; //品牌
			} else {
				category = 2; //功能
			};
			//belong dog、cat、pet
			belong = $(this).parents('.down_menu_right').attr('belongto');
			//商标或者功能id
			data_id = $(this).data('id');
			//商标或者功能text
			data_text = $(this).text();
			//跳页传参数
			window.location.href = '/pcpet/product.html?belong=' + belong + '&category=' + category + '&data_id=' + data_id + '&data_text=' + data_text;
		})
	},
	//从cookie中login_status 判断是否登陆
	is_login: function() {
		var login_status = document.cookie;
		login_status = login_status.split('=')[1];
		return login_status;
	},
	//获取cookie
	getCookie: function(cname) {
		var name = cname + "=";
		var ca = document.cookie.split(';');
		for(var i = 0; i < ca.length; i++) {
			var c = ca[i];
			while(c.charAt(0) == ' ') c = c.substring(1);
			if(c.indexOf(name) != -1) return c.substring(name.length, c.length);
		}
		return "";
	},
	//设置cookie
	setCookie: function(cname, cvalue, exdays) {
		var d = new Date();
		d.setTime(d.getTime() + (exdays * 24 * 60 * 60 * 1000));
		var expires = "expires=" + d.toUTCString();
		console.info(cname + "=" + cvalue + "; " + expires);
		document.cookie = cname + "=" + cvalue + "; " + expires;
		console.info(document.cookie);
	},
	//清除cookie
	clearCookie: function(username) {
		this.setCookie(username, "", -1);
	},
	//根据cookie login_status 判断是否登录
	cookie_login: function() {
		var self = this
		var uid = self.getCookie('uid')
		if(uid!='') {
			var quit_le;
			quit_le = '<a class="login quit rg" href="javascript:void(0)">退出</a>';
			$('.login, .rigester').remove();
			$('.top_bar_rg').prepend(quit_le);
			//获取购物车商品数量
			$.ajax({
				type: "post",
				url: 'http://www.ibabypet.com.cn/public/index.php/api/goods/cart_list/',
				async: true,
				data: {
					uid: uid
				},
				success: function(res) {
					res = JSON.parse(res);
					if(res.code == 200) {
						var shop_car_num = res.data.length;
						$('.top_bar_rg .shop_car span').text(shop_car_num);
					}
				}
			});
			$(document).on('click', '.quit', function() {
				self.clearCookie('uid')
				var aaa = self.getCookie('uid')
				console.log('aaa', aaa)
				//刷新当前页面
				window.location.reload();
			})
		}else{
			window.location.href="http://www.ibabypet.com.cn/pcpet/login.html"
		}
	},
	//点击头部的购物车按钮
	topClick: function() {
		var that = this;
		//点击购物车
		$('.j_shop_car').on('click', function(event) {
			event.preventDefault();
			//判断是否登陆
			if(that.getCookie('uid')!='') {
				location.href = 'shopCar.html';
			}else{
				location.href = 'login.html'
			}
		})
		//点击我的账户
		$('.j_my_account').on('click', function(event) {
			event.preventDefault();
			//判断是否登陆
			if(that.getCookie('uid')!='') {
				location.href = 'my_account.html';
			}else{
				location.href = 'login.html'
			}
		})
	}

};